import threading
import logging

from mon_center_utils import decode

logger = logging.getLogger('center.data_processor')

class DataProcessor(threading.Thread):
    """retrive data from the queue, do some trival convertion then 
    pass to the next processor
    """

    is_daemon = True

    def __init__(self, queue, next_processor):
        threading.Thread.__init__(self)
        if self.is_daemon:
            self.setDaemon(True)

        self.temp_data_store = {}
        #self.queue = config['recv_queue']
        self.queue = queue
        #self.next_processor = MonRWHandler(config['max_cache_size'])
        #self.next_processor = NextProcessor(args_to_next)
        self.next_processor = next_processor

    def run(self):
        logger.info('data-processor is now serving')
        if hasattr(self.next_processor, 'start'):
            self.next_processor.start()
        while True:
            #print 'reading data from queue'
            #print 'qsize: %d' %self.queue.qsize()
            data = self.queue.get(True)
            #print 'qsize: %d' %self.queue.qsize()
            #print data
            host = data[1][0]
            logger.debug('get 1 msg from %s'% host)
            #print data[0]
            self.event_filter(data)
            self.passon(data)

    def passon(self, data):
        msg = decode(data[0])
        host = data[1][0]
        timestamp = msg['timestamp']
        del msg['timestamp']
        
        for name, val in msg.items():
            #name = metric['name']
            #val = metric['val']
            logger.debug("pass 1 msg to db_handler")
            self.next_processor.write(host, name, (timestamp, val))

    def event_filter(self, args):
        pass

